import Vue from "vue"
import Router from "vue-router"
import store from "./store"

Vue.use(Router)
const router = new Router({
  routes: [
    {
      path: "/",
      name: "home",
      component: () => import("./views/home/home.vue"),
      meta: { title: "主页", left: -1 },
      alise: "/home"
    },

    {
      path: "/user/login",
      name: "login",
      meta: { title: "", right: -1, leftTo: "home" },
      component: () => import("./views/user/login.vue")
    },
    {
      path: "/user/register",
      name: "register",
      meta: { title: "注册", right: -1 },
      component: () => import("./views/user/register.vue")
    },

    {
      path: "/user",
      name: "userCenter",
      meta: {
        title: "个人中心",
        right: -1,
        requiresAuth: true
      },
      component: () => import("./views/user/userCenter.vue")
    },
    {
      path: "/user/addrlist",
      name: "addrList",
      meta: { title: "地址列表", right: -1, requiresAuth: true },
      component: () => import("./views/user/addrList.vue")
    },
    {
      path: "/user/addrlist/edit",
      name: "addrEdit",
      meta: { title: "地址编辑", right: -1, requiresAuth: true },
      component: () => import("./views/user/AddrEdit.vue")
    },
    {
      path: "/user/cart",
      name: "cart",
      meta: { title: "购物车", right: -1, requiresAuth: true },
      component: () => import("./views/items/cart.vue")
    },
    {
      path: "/user/reviews/:customer_id",
      name: "reviews",
      meta: { title: "历史评价", right: -1, requiresAuth: true },
      component: () => import("./views/user/reviews.vue")
    },
    {
      path: "/user/cart/submit",
      name: "submit",
      meta: { title: "确认订单", right: -1, requiresAuth: true },
      props: { totalPrice: 0.0 },
      component: () => import("./views/items/submit.vue")
    },
    {
      path: "/user/orderlist",
      name: "orderlist",
      meta: { title: "订单列表", right: -1, requiresAuth: true },
      component: () => import("./views/order/list.vue")
    },
    {
      path: "/user/orderlist/:order_id",
      name: "detail",
      meta: { title: "订单详情", right: -1, requiresAuth: true },
      component: () => import("./views/order/detail.vue")
    },
    {
      path: "/user/orderlist/:order_id/review/:item_id",
      name: "review",
      meta: { title: "评价", right: -1, requiresAuth: true },
      component: () => import("./views/order/review.vue")
    },

    {
      path: "/item/:item_id",
      name: "item",
      meta: { title: "商品详情" },
      component: () => import("./views/items/item.vue")
    },
    {
      path: "/search",
      name: "searchResult",
      meta: { title: "搜索结果" },
      component: () => import("./views/items/searchResult.vue"),
      beforeRouteUpdate: next => {
        next()
      }
    },

    {
      path: "/shop/login",
      name: "shopLogin",
      meta: { title: "", right: -1, leftTo: "home" },
      component: () => import("./views/shop/login.vue")
    },
    {
      path: "/shop/register",
      name: "shopRegister",
      meta: { title: "店铺注册", right: -1 },
      component: () => import("./views/shop/register.vue")
    },
    {
      path: "/shop/management",
      name: "shopArrange",
      meta: {
        title: "店铺管理",
        requiresShopAuth: true,
        leftTo: "home",
        right: -1
      },
      component: () => import("./views/shop/shopArrange.vue")
    },
    {
      path: "/shop/:shop_id",
      name: "shop",
      meta: { title: "店铺主页" },
      component: () => import("./views/shop/shop.vue")
    },
    {
      path: "/shop/edititem/:editType/:item_id",
      name: "edititem",
      meta: { title: "添加商品", right: -1 },
      component: () => import("./views/shop/edititem.vue")
    },
    {
      path: "/shop/orderList/:shop_id",
      name: "shopOrderList",
      meta: { title: "订单列表", right: -1 },
      component: () => import("./views/shop/shopOrderList.vue")
    }
  ]
})

import { axios, API_URL } from "./api"

router.beforeEach((to, from, next) => {
  if (from.name == null) {
    store.commit("SET_TRANSITION", "")
  } else {
    let toDepth = to.path.split("/").length
    let fromDepth = from.path.split("/").length
    if (to.path == "/") toDepth -= 1
    if (from.path == "/") fromDepth -= 1

    if (toDepth < fromDepth) store.commit("SET_TRANSITION", "right")
    else if (toDepth > fromDepth) store.commit("SET_TRANSITION", "left")
  }

  if (to.matched.some(record => record.meta.requiresAuth)) {
    let token = store.state.customer_token
    if (token) {
      axios
        .get(API_URL + "customer", {
          headers: { authorization: token }
        })
        .then(next)
        .catch(() => {
          next({ name: "login" })
        })
    } else {
      next({ name: "login" })
    }
  } else if (to.matched.some(record => record.meta.requiresShopAuth)) {
    let token = store.state.shop_token
    if (token) {
      axios
        .get(API_URL + "shop", {
          headers: { authorization: token }
        })
        .then(next)
        .catch(() => {
          next({ name: "shopLogin" })
        })
    } else {
      next({ name: "shopLogin" })
    }
  } else {
    next()
  }
})

export default router
